package com.dji.sample.component.listener.handle;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.dji.sample.common.constant.CommonConstant;
import com.dji.sample.component.listener.RedisMsgHandle;
import com.dji.sample.component.redis.RedisOpsUtils;
import com.dji.sample.component.websocket.config.ConcurrentWebSocketSession;
import com.dji.sample.component.websocket.model.BizCodeEnum;
import com.dji.sample.component.websocket.model.CustomWebSocketMessage;
import com.dji.sample.component.websocket.service.ISendMessageService;
import com.dji.sample.component.websocket.service.IWebSocketManageService;
import com.dji.sample.manage.model.entity.FireCheckRecordEntity;
import com.dji.sample.manage.model.enums.UserTypeEnum;

import java.util.Collection;

/**
 * @author yjt
 * @date 2023-02-17
 * @description:
 */
public class FireInfoHandle implements RedisMsgHandle {

    @Override
    public void dealMsg(String msg, String topic, ISendMessageService sendMessageService, IWebSocketManageService webSocketManageService, RedisOpsUtils redisOps) {
        Collection<ConcurrentWebSocketSession> webSocketSessions = webSocketManageService.getValueWithWorkspaceAndUserType(CommonConstant.DEFAULT_WORK_SPACE_ID, UserTypeEnum.WEB.getVal());
        if (webSocketSessions.isEmpty()) {
            return;
        }
        FireCheckRecordEntity redisLiveReceive = JSONObject.parseObject(JSON.parse(msg).toString(), FireCheckRecordEntity.class);
        CustomWebSocketMessage<Object> message =
                CustomWebSocketMessage.builder()
                        .timestamp(System.currentTimeMillis())
                        .bizCode(BizCodeEnum.FIRE_INFO.getCode())
                        .data(redisLiveReceive)
                        .build();
        sendMessageService.sendBatch(UserTypeEnum.WEB.getVal(), webSocketSessions, message);
    }
}
